package config

import (
	"os"

	"github.com/sirupsen/logrus"
)

var Log *logrus.Logger

func InitLogger() {

	logger := logrus.New()
	// 创建或打开日志文件
	file, err := os.OpenFile(ConfigInstance.Logger.File, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
	if err != nil {
		logrus.Fatal("Failed to log to file, using default stderr")
	}

	// 配置 logrus
	logger.SetOutput(file)
	if ConfigInstance.Logger.Format == "text" {
		logger.SetFormatter(&logrus.TextFormatter{
			TimestampFormat: "2006-01-02 15:04:05",
			FullTimestamp:   true,
			DisableColors:   true,
		})
	} else {
		logger.SetFormatter(&logrus.JSONFormatter{
			TimestampFormat: "2006-01-02 15:04:05",
		})
	}

	logger.SetLevel(logrus.Level(ConfigInstance.Logger.Level))

	Log = logger
}
